package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.BannerImg;
import cn.stylefeng.guns.modular.mapper.BannerImgMapper;
import cn.stylefeng.guns.modular.service.IBannerImgService;
import cn.stylefeng.guns.modular.util.IdWorker;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * <p>
 * 广告图片表 服务实现类
 * </p>
 *
 * @author XiangTianGe
 * @since 2020-04-15
 */
@Service
public class BannerImgServiceImpl extends ServiceImpl<BannerImgMapper, BannerImg> implements IBannerImgService {

    @Autowired
    private BannerImgMapper bannerImgMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 获取banner 列表
     */
    @Override
    public LayuiPageInfo getList(Integer type, Page page) {
        // 查询条件
        QueryWrapper qw = new QueryWrapper();
        if (type != null) {
            qw.eq("type", type);
        }
        qw.orderByDesc("updated_at");
        IPage iPage = bannerImgMapper.selectPage(page, qw);
        return LayuiPageFactory.createPageInfo(iPage);
    }

    /**
     * 跳转banner列表
     */
    @Override
    public ModelAndView edit(Long id) {
        ModelAndView modelAndView = new ModelAndView();
        BannerImg banner = bannerImgMapper.selectById(id);
        modelAndView.addObject("banner", banner);
        return modelAndView;
    }

    /**
     * 新增banner
     */
    @Override
    public ResponseData addBanner(BannerImg bannerImg) {
        bannerImg.setCreatedAt(new Date());
        bannerImg.setUpdatedAt(bannerImg.getCreatedAt());
        bannerImg.setId(idWorker.nextId());
        bannerImgMapper.insert(bannerImg);
        return ResponseData.success();
    }

    /**
     * 修改banner
     */
    @Override
    public ResponseData editBanner(BannerImg bannerImg) {
        bannerImg.setUpdatedAt(bannerImg.getCreatedAt());
        bannerImgMapper.updateById(bannerImg);
        return ResponseData.success();
    }

    /**
     * 删除banner
     */
    @Override
    public ResponseData delete(Long id) {
        bannerImgMapper.deleteById(id);
        return ResponseData.success();
    }

    /**
     * 通过id 查询数据
     */

    @Override
    public ResponseData queryById(Long id) {
        BannerImg bannerImg = bannerImgMapper.selectById(id);
        return ResponseData.success(bannerImg);
    }
}
